/**
 * 简历预览
 */
import React from 'react';
import { Icon, Spin } from 'antd';
import UploadOss from '@/components/UploadOss';
import cookie from 'js-cookie';
import { ResumeIframe } from '../../components';

export default function ResumePreview({ previewUrl = '', onChange = () => {}, onPreviewChange = () => {} }) {
    const [uploading, setUploading] = React.useState(false);
    const [fileList, setFileList] = React.useState();

    const isNative = /\.pdf(\?.*)?$/.test(previewUrl);

    const url = isNative
        ? previewUrl
        : `https://view.officeapps.live.com/op/view.aspx?src=${encodeURIComponent(previewUrl)}`;

    const handleUpload = async _res => {
        // 服务端实现兼容，暂不建议封装。
        fetch('/api/resource/oss/produceGetPreSignUrls', {
            body: JSON.stringify({
                sysCode: 'OA',
                fileList: _res
            }),
            method: 'POST',
            headers: {
                Accept: 'application/json',
                'Content-Type': 'application/json; charset=utf-8',
                Authorization: cookie.get('Authorization')
            }
        })
            .then(response => response.json())
            .then(myJson => {
                const { data } = myJson;
                const { fileList: files = [] } = data || {};
                onPreviewChange(files[0]?.preUrl);
                setUploading(false);
            });
    };

    return (
        <>
            {!previewUrl && (
                <Spin spinning={uploading}>
                    <UploadOss
                        style={{ width: '420px', height: '195px', margin: '50px auto 100px' }}
                        dragAble
                        uploadUrl="/api/resource/public/oss/producePutPreSignUrls"
                        sysCode="OA"
                        categoryCode="originalResumeFile"
                        onUploaded={handleUpload}
                        onChange={({ file = {} }) => {
                            if (file.status === 'uploading') {
                                setUploading(true);
                            }
                        }}
                        fileList={fileList}
                        setFileList={(_fileList, uploadResourceResult) => {
                            setFileList(_fileList);
                            onChange(uploadResourceResult);
                        }}
                        checkFileSize={50}
                        checkFileType="pdf、doc、xls、docx、wps"
                        checkFileTypeMsg="只能上传pdf、doc、xls、docx、wps格式的文件！"
                        checkFileSizeMsg="文件大小不能超过50MB！"
                        showUploadList={false}
                        showPreviewFile
                    >
                        <p className="ant-upload-drag-icon">
                            <Icon type="inbox" />
                        </p>
                        <p className="ant-upload-text">点击或拖拽简历到此处上传</p>
                        <p className="ant-upload-hint">支持 pdf、doc、xls、docx、wps 等简历格式</p>
                    </UploadOss>
                </Spin>
            )}

            {previewUrl && <ResumeIframe url={url} isNative={isNative} />}
        </>
    );
}
